package com.bafa.services.scorers;

import java.sql.ResultSet;

import com.bafa.dbcache.LeagueCache;
import com.bafa.debug.Debug;
import com.bafa.jdbc.DBAccess;
import com.bafa.jdbc.QueryObject;
import com.bafa.jdbc.UpdateObject;
import com.bafa.objects.Match;
import com.bafa.objects.Message;
import com.bafa.objects.player.PlayerMatch;
import com.bafa.objects.season.Division;
import com.bafa.objects.team.Team;
import com.bafa.objects.team.TeamSeasonStats;
import com.bafa.services.RetrieveServices;

public class ScorersServices {

	public static Message deleteScorers(int matchId) {
		Message message = new Message();
		Match match = RetrieveServices.getMatch(matchId);
		for (int i = 0; i < match.scorersA().length; i++) {
			FunctionalScorerServices.deleteScorer(match.scorersA()[i], matchId,
					match.getTeamA());
		}
		for (int i = 0; i < match.scorersB().length; i++) {
			FunctionalScorerServices.deleteScorer(match.scorersB()[i], matchId,
					match.getTeamB());
		}
		message.addMessage("All Done");

		return message;
	}
	
	public static Message addScorers(String[] scorers, int matchId,String[] goals,int teamId,boolean home){
		Message message = new Message();
		Match match = RetrieveServices.getMatch(matchId);
		DBAccess db = new DBAccess();
		try{
			for (int i = 0; i < scorers.length; i++) {
				PlayerMatch playerMatch = RetrieveServices.getPlayer(scorers[i]).getMatchGoals().get(matchId);
				if(playerMatch!=null){
					if(playerMatch.getGoals()!=new Integer(goals[i]).intValue()){
						FunctionalScorerServices.deleteScorer(scorers[i],matchId,teamId);
						FunctionalScorerServices.addScorer(db,RetrieveServices.getMatch(matchId),scorers[i],goals[i],RetrieveServices.getTeam(""+teamId),home);		
					}
				}else{
					FunctionalScorerServices.addScorer(db,RetrieveServices.getMatch(matchId),scorers[i],goals[i],RetrieveServices.getTeam(""+teamId),home);
				}
				
			}
		}catch(Exception e){
			message.addError("Unexpected error. Scorer has not been updated");
			Debug.printStackTrace(e);
		}
		return message;
	}

}
